package com.json.hellojson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.json.entity.Book;
import com.json.entity.MyBook;
import com.json.entity.MyAuthor;

public class ReadeJsonObjectDemo {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws JSONException, IOException {
		// TODO Auto-generated method stub

		String jstr="{\"bookId\":\"1\",\"bookPrice\":\"12.3\",\"bookName\":\"hhe\"},{\"bookId\":\"2\",\"bookPrice\":\"45.6\",\"bookName\":\"Hello\"}";
		JSONObject jo=new JSONObject(jstr);
		System.out.println(jo.get("bookId"));
		
//		JSONArray ja=new JSONArray();
//		JSONObject jo1= ja.getJSONObject(0);
//	1method	
		ObjectMapper mapper=new ObjectMapper();
		List<Book> list=new ArrayList<Book>();
		list.add(new Book("001","kk","56.3"));
		list.add(new Book("002","hhhh","598.3"));
		String content=mapper.writeValueAsString(list);
		List<LinkedHashMap<String, String>> listn=mapper.readValue(content, List.class);
		System.out.println(listn);
		System.out.println(listn.get(0).get("bookId"));
//	2method	
		//不定长度
//		List<MyBook> list1=new ArrayList<MyBook>();
//		list1.add(new MyBook("001","kk","56.3",new MyAuthor("1","ee","zhuhai",list1)));
//		list1.add(new MyBook("002","hhhh","598.3",new MyAuthor("2","ff","zhuhai",list1)));
//		String content1=mapper.writeValueAsString(list1);
		JavaType type=mapper.getTypeFactory().constructParametricType(List.class, Book.class);
//		List<Book> list2=mapper.readValue(content1, type);
//		System.out.println(list2.get(0).getBookId());
		
		
	}

}
